package com.it.park.util.email;

import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailUtil {
	private static String host = "smtp.163.com";
	private static String sender = "testcenter321@163.com";
	private static String username = "testcenter321";
	private static String password = "qazxsw";
	private static String receiver = "1280453165@qq.com";
	private static int port = 25;
	private static Properties pro = new Properties();
	static{
		pro.put("mail.smtp.host", host);
		pro.put("mail.smtp.auth", "true");
	}
	
	public static boolean sendEmail(String content){
		try {
				/*Session session=Session.getInstance(pro,new Authenticator() {
		
					@Override
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(username,password.toCharArray());
					}
					
				});*/
			Session session=Session.getInstance(pro);

				
				 //封装Message对象
				session.setDebug(true);
		        Message message=new MimeMessage(session);
		        message.setFrom(new InternetAddress(sender)); //发件人
				message.setRecipient(Message.RecipientType.TO, new InternetAddress(receiver));
				message.setSubject("意见反馈");
				//设置内容(设置字符集处理乱码问题)
				message.setContent(content,"text/html;charset=utf-8");
		        message.setSentDate(new Date());
		      //创建Transport实例，发送邮件
		        Transport tran=session.getTransport("smtp");
		        tran.connect(host, username, password);
		        tran.sendMessage(message,message.getAllRecipients());
		        tran.close();
		        return true;
		} catch (AddressException e) {
			e.printStackTrace();
			return false;
		} catch (MessagingException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean sendEmail(String subiect,String receiver,String content){
		try {
			/*Session session=Session.getInstance(pro,new Authenticator() {
				@Override
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication(username,password.toCharArray());
				}
			});*/
			Session session=Session.getInstance(pro);
			//封装Message对象
			session.setDebug(true);
			Message message=new MimeMessage(session);
			message.setFrom(new InternetAddress(sender)); //发件人
			message.setRecipient(Message.RecipientType.TO, new InternetAddress(receiver));
			message.setSubject(subiect);
			//设置内容(设置字符集处理乱码问题)
			message.setContent(content,"text/html;charset=utf-8");
			message.setSentDate(new Date());
			//创建Transport实例，发送邮件
			Transport tran=session.getTransport("smtp");
			tran.connect(host, username, password);
			tran.sendMessage(message,message.getAllRecipients());
			tran.close();
			return true;
		} catch (AddressException e) {
			e.printStackTrace();
			return false;
		} catch (MessagingException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static void main(String[] args) throws Exception {
		EmailUtil.sendEmail("哈哈");
}

}
